﻿using PatientRegisterService.DAO;
using PatientRegisterService.Model;
using PatientRegisterService.Utils;
using System;
using System.Collections.Generic;

namespace PatientRegisterService.Service
{
    public class PatientRegistService
    {
        PatientRegistDAO patientRegisterDAO = new PatientRegistDAO();

        /// <summary>
        /// 号源查询数据组织
        /// </summary>
        /// <param name="beginDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="YSCode">医生工号</param>
        /// <param name="KSCode">科室号</param>
        /// <param name="numType">号源类型</param>
        /// <param name="isRemain">是否有剩余号</param>
        /// <returns></returns>
        public List<NumSourceInfo> GetOrderNumInfo(string beginDate, string endDate, string YSCode, string KSCode, string numType, bool isRemain)
        {

            List<NumSourceInfo> numSourceInfosList = patientRegisterDAO.GetOrderNumInfoDAO(beginDate, endDate, YSCode, KSCode, isRemain);

            List<NumSourceInfo> result = new List<NumSourceInfo>();

            foreach (NumSourceInfo numSourceInfo in numSourceInfosList)
            {
                numSourceInfo.scheduleDate = numSourceInfo.scheduleDate.Substring(0, 10);

                bool flag = numType.Equals("%") ? true : numSourceInfo.orderNumType.Equals(numType);

                if (numSourceInfo.timeRange.Equals("上午") && flag)
                {
                    numSourceInfo.startTime = "08:00";
                    numSourceInfo.endTime = "12:00";
                    numSourceInfo.scheduleId = numSourceInfo.scheduleId;
                    numSourceInfo.numSourceId = numSourceInfo.scheduleId + DateTime.Now.ToString("yyyyMMddHHmmssms") + "_1";
                    numSourceInfo.remainNum = DistributionHelper.isToday(numSourceInfo.remainNum, numSourceInfo.scheduleDate);
                    numSourceInfo.timeRange = "1";
                    result.Add(numSourceInfo);
                }

                if (numSourceInfo.timeRange.Equals("下午") && flag)
                {
                    numSourceInfo.startTime = "14:00";
                    numSourceInfo.endTime = "18:00";
                    numSourceInfo.scheduleId = numSourceInfo.scheduleId;
                    numSourceInfo.numSourceId = numSourceInfo.scheduleId + DateTime.Now.ToString("yyyyMMddHHmmssms") + "_3";
                    numSourceInfo.remainNum = DistributionHelper.isToday(numSourceInfo.remainNum, numSourceInfo.scheduleDate);
                    numSourceInfo.timeRange = "2";
                    result.Add(numSourceInfo);
                }

                if (numSourceInfo.timeRange.Equals("上午及下午") && flag)
                {
                    numSourceInfo.timeRange = "上午";
                    numSourceInfo.startTime = "08:00";
                    numSourceInfo.endTime = "12:00";
                    //numSourceInfo.remainNum = DistributionHelper.allotMorningAndAfternoon(numSourceInfo.remainNum, "上午", numSourceInfo.scheduleDate);
                    numSourceInfo.scheduleId = numSourceInfo.scheduleId;
                    numSourceInfo.numSourceId = numSourceInfo.scheduleId  + DateTime.Now.ToString("yyyyMMddHHmmssms") + "_1";
                    numSourceInfo.timeRange = "1";
                    result.Add(numSourceInfo);

                    NumSourceInfo numSourceInfoCopy = new NumSourceInfo();
                    numSourceInfoCopy.hosOrgCode = numSourceInfo.hosOrgCode;
                    numSourceInfoCopy.orderType = numSourceInfo.orderType;
                    numSourceInfoCopy.resourceCode = numSourceInfo.resourceCode;
                    numSourceInfoCopy.hosName = numSourceInfo.hosName;
                    numSourceInfoCopy.oneDeptName = numSourceInfo.oneDeptName;
                    numSourceInfoCopy.deptName = numSourceInfo.deptName;
                    numSourceInfoCopy.resourceName = numSourceInfo.resourceName;
                    numSourceInfoCopy.visitCost = numSourceInfo.visitCost;
                    numSourceInfoCopy.scheduleDate = numSourceInfo.scheduleDate;
                    numSourceInfoCopy.timeRange = "下午";
                    numSourceInfoCopy.startTime = "14:00";
                    numSourceInfoCopy.endTime = "18:00";
                    numSourceInfoCopy.remainNum = numSourceInfo.remainNum;
                    //numSourceInfoCopy.remainNum = DistributionHelper.allotMorningAndAfternoon(numSourceInfo.remainNum, "下午", numSourceInfo.scheduleDate);
                    numSourceInfoCopy.scheduleId = numSourceInfo.scheduleId;
                    numSourceInfoCopy.numSourceId = numSourceInfoCopy.scheduleId + "_3";
                    numSourceInfoCopy.timeRange = "2";
                    result.Add(numSourceInfoCopy);
                }

                if (numSourceInfo.timeRange.Equals("全天") && flag)
                {
                    numSourceInfo.timeRange = "上午";
                    numSourceInfo.startTime = "08:00";
                    numSourceInfo.endTime = "12:00";
                    numSourceInfo.remainNum = DistributionHelper.allotAllDay(numSourceInfo.remainNum, "上午", numSourceInfo.scheduleDate);
                    numSourceInfo.scheduleId = numSourceInfo.scheduleId;
                    numSourceInfo.numSourceId = numSourceInfo.scheduleId + DateTime.Now.ToString("yyyyMMddHHmmssms") + "_1";
                    numSourceInfo.timeRange = "1";
                    result.Add(numSourceInfo);

                    NumSourceInfo numSourceInfoCopy = new NumSourceInfo();
                    numSourceInfoCopy.hosOrgCode = numSourceInfo.hosOrgCode;
                    numSourceInfoCopy.orderType = numSourceInfo.orderType;
                    numSourceInfoCopy.resourceCode = numSourceInfo.resourceCode;
                    numSourceInfoCopy.hosName = numSourceInfo.hosName;
                    numSourceInfoCopy.oneDeptName = numSourceInfo.oneDeptName;
                    numSourceInfoCopy.deptName = numSourceInfo.deptName;
                    numSourceInfoCopy.resourceName = numSourceInfo.resourceName;
                    numSourceInfoCopy.visitCost = numSourceInfo.visitCost;
                    numSourceInfoCopy.scheduleDate = numSourceInfo.scheduleDate;
                    numSourceInfoCopy.timeRange = "下午";
                    numSourceInfoCopy.startTime = "14:00";
                    numSourceInfoCopy.endTime = "18:00";
                    numSourceInfoCopy.remainNum = numSourceInfo.remainNum;
                    //numSourceInfoCopy.remainNum = DistributionHelper.allotAllDay(numSourceInfo.remainNum, "下午", numSourceInfo.scheduleDate);
                    numSourceInfoCopy.scheduleId = numSourceInfo.scheduleId;
                    numSourceInfoCopy.numSourceId = numSourceInfoCopy.scheduleId + DateTime.Now.ToString("yyyyMMddHHmmssms") + "_3";
                    numSourceInfoCopy.timeRange = "2";
                    result.Add(numSourceInfoCopy);

                    NumSourceInfo numSourceInfoCopy2 = new NumSourceInfo();
                    numSourceInfoCopy2.hosOrgCode = numSourceInfo.hosOrgCode;
                    numSourceInfoCopy2.orderType = numSourceInfo.orderType;
                    numSourceInfoCopy2.resourceCode = numSourceInfo.resourceCode;
                    numSourceInfoCopy2.hosName = numSourceInfo.hosName;
                    numSourceInfoCopy2.oneDeptName = numSourceInfo.oneDeptName;
                    numSourceInfoCopy2.deptName = numSourceInfo.deptName;
                    numSourceInfoCopy2.resourceName = numSourceInfo.resourceName;
                    numSourceInfoCopy2.visitCost = numSourceInfo.visitCost;
                    numSourceInfoCopy2.scheduleDate = numSourceInfo.scheduleDate;
                    numSourceInfoCopy2.timeRange = "晚上";
                    numSourceInfoCopy2.startTime = "18:00";
                    numSourceInfoCopy2.endTime = "08:00";
                    numSourceInfoCopy2.remainNum = numSourceInfo.remainNum;
                    //numSourceInfoCopy2.remainNum = DistributionHelper.allotAllDay(numSourceInfo.remainNum, "晚上", numSourceInfo.scheduleDate);
                    numSourceInfoCopy2.scheduleId = numSourceInfo.scheduleId;
                    numSourceInfoCopy2.numSourceId = numSourceInfoCopy.scheduleId + DateTime.Now.ToString("yyyyMMddHHmmssms") + "_4";
                    numSourceInfoCopy2.timeRange = "3";
                    result.Add(numSourceInfoCopy2);
                }
            }
            return result;
        }

        /// <summary>
        /// 号源预约
        /// </summary>
        /// <param name="CallNo"></param>
        /// <param name="CallName"></param>
        /// <param name="ReservationDate"></param>
        /// <param name="ScheduleId"></param>
        /// <param name="ScheduleIdStr"></param>
        /// <param name="CardNo"></param>
        /// <param name="CardType"></param>
        /// <param name="IdCardNo"></param>
        /// <param name="Name"></param>
        /// <param name="Sex"></param>
        /// <param name="BirthDay"></param>
        /// <param name="Tel"></param>
        /// <param name="Adress"></param>
        /// <param name="TransNo"></param>
        /// <param name="errorMessage"></param>
        /// <param name="KeyNo"></param>
        /// <param name="payMode"></param>
        /// <param name="patient_type"></param>
        /// <param name="patient_id"></param>
        /// <param name="userCardType"></param>
        /// <param name="replaceUserCardType"></param>
        /// <param name="replaceUserCardId"></param>
        /// <param name="replaceUserName"></param>
        /// <param name="ipAddr"></param>
        /// <param name="macAddr"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="userCardId"></param>
        /// <returns></returns>
        public bool SubmitOrderByUserInfo(string CallNo, string CallName, DateTime ReservationDate, int ScheduleId, string ScheduleIdStr, string CardNo, int CardType, string IdCardNo, string Name, int Sex, DateTime BirthDay, string Tel, string Adress, string TransNo, out string errorMessage, out int KeyNo,
            string payMode, string patient_type, string patient_id, string userCardType, string replaceUserCardType, string replaceUserCardId, string replaceUserName, string ipAddr, string macAddr, string startTime, string endTime, string userCardId)
        {
            return patientRegisterDAO.SubmitOrderByUserInfoADO(CallNo, CallName, ReservationDate, ScheduleId, ScheduleIdStr, CardNo, CardType, IdCardNo, Name, Sex, BirthDay, Tel, Adress, TransNo, out errorMessage, out KeyNo, payMode, patient_type, patient_id, userCardType, replaceUserCardType, replaceUserCardId, replaceUserName, ipAddr, macAddr, startTime, endTime, userCardId);
        }

        /// <summary>
        /// 预约退号
        /// </summary>
        /// <param name="OrderId"></param>
        /// <param name="DelTransNo"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public bool OrderCancelInfo(int OrderId, string DelTransNo, out string errorMessage)
        {
            return patientRegisterDAO.OrderCancelInfoADO(OrderId, DelTransNo, out errorMessage);
        }

        /// <summary>
        /// 预约单列表查询
        /// </summary>
        /// <param name="queryPersonType"></param>
        /// <param name="userCardType"></param>
        /// <param name="userCardId"></param>
        /// <param name="queryDateType"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<OrderInfo> GetOrderList(string queryPersonType, string userCardType, string userCardId, string queryDateType, DateTime startTime, DateTime endTime)
        {
            return patientRegisterDAO.GetOrderListADO(queryPersonType, userCardType, userCardId, queryDateType, startTime, endTime);
        }

        /// <summary>
        /// 预约单详情查询
        /// </summary>
        /// <param name="OrderId"></param>
        /// <returns></returns>
        public OrderDetailsInfo GetOrderGetOrderDetailInfo(int OrderId)
        {
            return patientRegisterDAO.GetOrderGetOrderDetailInfoADO(OrderId);
        }

        /// <summary>
        /// 可预约医生信息/可预约门诊信息
        /// </summary>
        /// <param name="DeptCode">科室代码</param>
        /// <param name="Flag"></param>
        /// <returns></returns>
        public List<ResourceDoct> GetResourceDoctInfo(string DeptCode, int Flag)
        {

            return patientRegisterDAO.GetResourceDoctInfoADO(DeptCode, Flag);
        }

        /// <summary>
        /// 费用变更接口
        /// </summary>
        /// <param name="OrderId"></param>
        /// <param name="BankTransNo"></param>
        /// <param name="PayMode"></param>
        /// <param name="PayAmount"></param>
        /// <param name="PayType0"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public bool UpPayStatus(int OrderId, string BankTransNo, string PayMode, decimal PayAmount, string PayType0, out string errorMessage)
        {
            return patientRegisterDAO.UpPayStatusADO(OrderId, BankTransNo, PayMode, PayAmount, PayType0, out errorMessage);
        }
    }
}